home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / tkern091.zip / SRC / MISC.C < prev    next >
Text File  |  1994-03-05  |  3KB  |  109 lines

  1. /*
  2.  *  This file forms part of "TKERN" - "Troy's Kernel for Windows".
  3.  *
  4.  *  Copyright (C) 1994  Troy Rollo <troy@cbme.unsw.EDU.AU>
  5.  *
  6.  *  This library is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU Library General Public
  8.  *  License as published by the Free Software Foundation; either
  9.  *  version 2 of the License, or (at your option) any later version.
  10.  *
  11.  *  This library is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  *  Library General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU Library General Public
  17.  *  License along with this library; if not, write to the Free
  18.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <windows.h>
  22. #include <toolhelp.h>
  23. #include <stdlib.h>
  24. #include <memory.h>
  25. #include <string.h>
  26. #include <alloc.h>
  27. #include <stdarg.h>
  28. #include <errno.h>
  29. #include <sys/tfile.h>
  30. #include <sys/task.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/wait.h>
  33. #include <sys/tkern.h>
  34.  
  35. int    nSleepers = 0;    // Number of sleeping tasks
  36.  
  37. /*
  38.  * We couldn't use PeekMessage because you can't seem to have 2 tkern tasks
  39.  * using PeekMessage loops at the same time. GetMessages puts the current task
  40.  * to sleep and marks it as sleeping. The file manager will issue a wakeup
  41.  * call when an event has occurred on which sleeping tasks are waiting.
  42.  * This will cause tkern to send a dummy message to all tasks which are sleeping.
  43.  * That will cause the GetMessage call to return
  44.  */
  45.  
  46. void    GetMessages(struct task *pt)
  47. {
  48.     MSG    msg;
  49.  
  50.     nSleepers++;
  51.     pt->nFlags |= TF_ASLEEP;
  52.     GetMessage(&msg, 0, 0, 0);
  53.     if (msg.message != TKWM_WAKEUP)
  54.     {
  55.         TranslateMessage(&msg);
  56.         DispatchMessage(&msg);
  57.     }
  58.     if (pt->nFlags & TF_ASLEEP)
  59.     {
  60.         nSleepers--;
  61.         pt->nFlags &= ~TF_ASLEEP;
  62.     }
  63. }
  64.  
  65. /* FlushMessages should be used only when there is no alternative */
  66. void    FlushMessages(void)
  67. {
  68.     MSG    msg;
  69.  
  70.     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  71.     {
  72.         TranslateMessage(&msg);
  73.         DispatchMessage(&msg);
  74.     }
  75. }
  76.  
  77.  
  78. /*
  79.  * This function is used by tkern_exec() to copy the argument and
  80.  * environment vectors.
  81.  */
  82.  
  83. void
  84. Copy_Array(char ***dest, char **source)
  85. {
  86.     int    i;
  87.     int    nBytes = sizeof(char *);
  88.     char    *pchBuffer;
  89.     char    **ppchVector;
  90.  
  91.     for (i = 0; source[i]; i++)
  92.     {
  93.         nBytes += strlen(source[i]) + 1;
  94.     }
  95.     nBytes += (i + 1) * sizeof(char *);
  96.     ppchVector = (char **) malloc(nBytes);
  97.     *dest = ppchVector;
  98.     pchBuffer = (char *) (ppchVector + i + 1);
  99.     while (*source)
  100.     {
  101.         *ppchVector++ = pchBuffer;
  102.         strcpy(pchBuffer, *source);
  103.         pchBuffer += strlen(pchBuffer) + 1;
  104.         source++;
  105.     }
  106.     *ppchVector = 0;
  107. }
  108.  
  109.